<?php

require('config.php');
require('constant.php');
require('common.php');
require('simple_html_dom.php');

Class auc_api Extends common {
	
	function get_watchlist($auc_uid){
		$url=sprintf(AUC_WATCH_YQL_BASE,$auc_uid,AUC_WATCH_YQL_TITLEIMG);
		$titleimg_obj=json_decode($this->retrieve(YQL.urlencode($url)));
		$url=sprintf(AUC_WATCH_YQL_BASE,$auc_uid,AUC_WATCH_YQL_PRICE);
		$price_obj=json_decode($this->retrieve(YQL.urlencode($url)));

		$p_arr = $price_obj->query->results->p;
		$watchlist_arr=array();
		$i=0;
		foreach($titleimg_obj->query->results->a as $obj){
			$ret=array();
			$ret['href']=$obj->href;
			$ret['img']=$obj->img->src;
			$ret['title']=$obj->img->title;
			$ret['price']=$p_arr[$i];
			array_push($watchlist_arr,$ret);
			$i++;
		}
		return ($watchlist_arr);
	}

	function get_search_rss($q,$params){
		$url=sprintf(AUC_SEARCHRSS_YQL_BASE,$q,http_build_query($params));
		$search_obj=json_decode($this->retrieve(YQL.urlencode($url)));
		$search_arr=array();
		if(!isset($search_obj->query->results->item) and empty($search_obj->query->results->item)){
		  return $search_arr;
		}
		foreach($search_obj->query->results->item as $obj){
			$ret=array();
			$ret['href']=$obj->link;
			$ret['title']=$obj->title;
			$desc=$obj->description;
			if(preg_match('!src=\"(.+)\" height.+<b>(.+)</b>!',$desc,$matches))
			{
				$ret['img']=$matches[1];
				$ret['price']=$matches[2];
			}else{
				$ret['img']='';
				$ret['price']='';
			}
			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}

  /* directly connect */
	function get_search_rss_d($q,$params){
		$url=sprintf(AUC_SEARCHRSS_DIRECT,$q,http_build_query($params));
		//echo $this->auc($url);
		$xml = new SimpleXMLElement($this->retrieve($url));
		$search_obj_arr=$xml->xpath('channel/item'); 
		$search_arr=array();
		foreach($search_obj_arr as $obj){
			$ret=array();
			$ret['href']=(string)$obj->link;
			$ret['title']=(string)$obj->title;
			$desc=$obj->description;
			if(preg_match('!src=\"(.+)\" height.+<b>(.+)</b>!',$desc,$matches))
			{
				$ret['img']=$matches[1];
				$ret['price']=$matches[2];
			}else{
				$ret['img']='';
				$ret['price']='';
			}
			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}

	function get_search_category($q,$params){
		$url=sprintf(AUC_SEARCH_YQL_BASE,$q,http_build_query($params),AUC_SEARCH_YQL_CATEGORY);
		$search_obj=json_decode($this->retrieve(YQL.urlencode($url)));
		$search_arr=array();
		foreach($search_obj->query->results->li as $obj){
			$ret=array();
			$ret['name'] = $obj->a->content;
			$ret['amount'] = str_replace(array('(',')'),'',$obj->span);
			if(preg_match('!&cat=(.+)!',$obj->a->href,$matches)){
				$ret['cat_id']=$matches[1];
			}else{
				$ret['cat_id']='';
			}

			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}

	function get_autocomplete($q){
		$url=sprintf(AUC_AUTOCOMPLETE_YQL_BASE,$q);
		$search_obj=json_decode($this->retrieve(YQL.urlencode($url)));
		$search_arr=array();
		foreach($search_obj->query->results->ResultSet->Result as $obj){
			$ret=array();
			$ret['title'] = $obj->Title;
			$ret['scale'] = $obj->Scale;

			array_push($search_arr,$ret);
		}
		return ($search_arr);
	}

	function get_buy_history($auc_uid){
		$url=sprintf(AUC_RATING_BUY,$auc_uid);
		$html = file_get_html($url);
		$buy_history_arr = array();
		foreach($html->find('p[class="prodinfo clearfix"]') as $node){
			$a = $node->find('a',0);
			$t = $node->find('span[class="fnt-85"]',0);
			$p = $node->find('span[class="price"]',0);
			$ret = array();
			$ret['href'] =  $a->href;
			$ret['title'] = $a->plaintext;
			$buy_time = $t->plaintext;
			if(preg_match('/(\d\d\d\d-\d\d-\d\d \d\d:\d\d)/', $buy_time, $matches)){
				$ret['buy_time'] = $matches[1];
			}else{
				$ret['buy_time'] = $buy_time;
			}
			$price = str_replace(",","",$p->plaintext);
			if(preg_match('/(\d+)/', $price, $matches)){
				$ret['price'] = $matches[1];
			}else{
				$ret['price'] = $price;
			}

			array_push($buy_history_arr,$ret);
		}
		$html->clear();
		unset($html);
		return ($buy_history_arr);
	}
}
?>
